str2date = function(c_date) {
    if (!c_date)
        return "";
    var tempArray = c_date.split("-");
    if (tempArray.length != 3) {
        alert("你输入的日期格式不正确,正确的格式:2000-05-01 02:54:12");
        return 0;
    }
    var dateArr = c_date.split(" ");
    var date = null;
    if (dateArr.length == 2) {
        var yymmdd = dateArr[0].split("-");
        var hhmmss = dateArr[1].split(":");
        date = new Date(yymmdd[0], yymmdd[1] - 1, yymmdd[2], hhmmss[0], hhmmss[1], hhmmss[2]);
    } else {
        date = new Date(tempArray[0], tempArray[1] - 1, tempArray[2], 00, 00, 01);
    }
    return date;
};

getnextdate = function(nowdate){
	var now = new Date(nowdate);
	var yy = now.getFullYear();
	var mm = now.getMonth()+1;
	var dd = now.getDate();
	var hh = now.getHours();
	var min = now.getMinutes();
	return str2date(yy+"-"+mm+"-"+dd+" "+hh+":"+min+":00");
};

var secondCounter = 0;

function change() {
	secondCounter ++;
	var forms = document.forms;
	var formCount = forms.length;
	for(i=0;i<formCount;i++){
		var fm = forms[i];
		var nowDate = fm.nowDate.value;
		var myDate = str2date(nowDate);
		var begin_time =fm.begin_time.value;
		var the_begin_time = str2date(begin_time);
		var nextbegin_time =fm.nextbegin_time.value;
		var nextbegin_time = str2date(nextbegin_time);
		var end_time =fm.end_time.value;
		var the_end_time = str2date(end_time);
		
		myDate = Number(myDate) + secondCounter * 1000; //当前时间
		
		var sid = fm.sid.value;	//秒杀奖品ID
		var seckillPrice = fm.price.value;	//秒杀奖品ID
		if(myDate<the_end_time){
			 
			 document.getElementById("surplus101"+fm.sid.value).value
			 var vv = $("#surplus101"+sid).val();
			var surplus101 = Number(vv);
			if(myDate>the_begin_time&&surplus101>0){
				fm.miaoshabutton.innerHTML = seckillPrice + "元秒杀";
				$('#miaoshabutton'+sid).removeAttr("disabled" );
				//fm.cjcontent1.innerHTML = "<p class=\"prizeCount\"><span class=\"redFont\">当前时段奖品剩余："+surplus101+"个</span></p>";
			}else if(myDate>the_begin_time&&surplus101==0){
				fm.miaoshabutton.innerHTML = "秒杀已结束";
				$('#miaoshabutton'+sid).attr("disabled","disabled");
				//$(".prizeInfo").hide();
				document.getElementById("cjcontent"+sid).innerHTML ="本次秒杀活动已结束，下次秒杀，敬请关注。";
			}else {
				fm.miaoshabutton.innerHTML = "秒杀未开始";
				$('#miaoshabutton'+sid).attr("disabled","disabled");
				document.getElementById("cjcontent"+sid).innerHTML = "<p class=\"prizeNext\">距下次秒杀还剩：<br/><span class=\"redFont\"><span id=\"showxmtime"+sid+"\"></span></span></p>";
				var seconds = (the_begin_time - myDate)/1000;
				var minutes = Math.floor(seconds/60);
				var hours = Math.floor(minutes/60);
				var days = Math.floor(hours/24);
				var day= days;
				var hour= hours % 24;
				var min= minutes % 60;
				var sec= seconds % 60;
				
				if (day < 0 || hour < 0 || min < 0 || sec < 0) {
					day = 0;
					hour = 0;
					min = 0;
					sec = 0;
					$("#showxmtime"+sid).html(day + "天" + hour + "时" + min + "分" + sec + "秒");
					if(surplus101>0){
						fm.miaoshabutton.innerHTML = seckillPrice+"元秒杀";
						document.getElementById("cjcontent"+sid).innerHTML = "<p class=\"prizeCount\"><span class=\"redFont\">当前鞢奔时段奖品剩余："+surplus101+"个</span></p>";
						clearInterval(djs);
					}else{
						var now = new Date(myDate);
						var end = new Date(the_end_time);
						var dd = now.getDate();
						var hh = now.getHours();
						var end_dd = end.getDate();
						//alert("now="+now+"  end="+end+"  dd="+dd+"   hh="+hh+"  end_dd="+end_dd);
						if(hh==23&&end_dd==dd+1){
							fm.miaoshabutton.innerHTML = "秒杀已经结束";
							$('#miaoshabutton'+sid).attr("disabled","disabled");
							//$("#prizeInfo1").hide();
							document.getElementById("cjcontent"+sid).innerHTML ="本次秒杀活动已结束，下次秒杀，敬请关注。";
						}else{
							if(fm.the_begin_time>=fm.nextbegin_time){
								fm.the_begin_time = new Date(fm.nextbegin_time.getTime()+30*60*1000);
							}else{
								fm.the_begin_time = fm.nextbegin_time;
							}
						}
					}
				} else {
					$("#showxmtime"+sid).html(day + "天" + hour + "时" + min + "分" + sec + "秒");
				}
			}
			
			
			
		}else{
			fm.miaoshabutton.innerHTML = "秒杀已结束";
			$('#miaoshabutton'+sid).attr("disabled","disabled");
			//$(".prizeInfo").hide();
			document.getElementById("cjcontent"+sid).innerHTML ="本次秒杀活动已结束，下次秒杀，敬请关注。";
		}
		
		
	}
	
	
}

var djs = setInterval("change()", 1000);

 
